<template>
	<view class="spay">
		<uni-nav-bar class="uni-nav-bar" color="#ffffff" left-icon="left" @clickLeft="togo" title="开通会员权益"
			:border="false" fixed statusBar></uni-nav-bar>
		<view class="headerview">
			<view class="huiyuanka">
				<text>开通会员 预计可省{{disnum}}元</text>
			</view>
		</view>
		<view class="listbox">
			<view class="list" @click="select = index" :class="{select: select == index}"
				v-for="(item,index) in paylist" :key="item.id">
				<view>一个月</view>
				<view class="price">￥<text>{{item.money}}</text></view>
				<view>享{{Number(item.credit).toFixed(2)}}元优惠额度</view>
			</view>
		</view>
		<!-- <view class="selectxieyi">
			<checkbox-group @change="aselect">
				<label>
					<checkbox :checked="isselect" color="#FFCC33" style="transform:scale(0.7)" />已阅读并同意
				</label>
			</checkbox-group>
			<view class="xieyi">《会员协议》</view>
		</view> -->
		<view class="btnpay" @click="send">立即开通</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				paylist: [],
				select: 0,
				isselect: false
			}
		},
		async onReady() {
			const data = await this.$apis.paymemberlimit.types({
				type: 'member_level'
			})
			console.log(data)
			this.paylist = data.data
		},
		computed: {
			disnum() {
				return Number(this.paylist[this.select]?.credit).toFixed(2)
			}
		},
		methods: {
			togo() {
				uni.navigateBack(-1)
			},
			aselect(e) {
				this.isselect = !this.isselect
			},
			async send() {
				console.log(this.isselect)
				// if (!this.isselect) return uni.showToast({
				// 	title: '请选择并阅读会员协议',
				// 	icon: 'none'
				// })
				const data = await this.$apis.paymemberlimit.recharge({
					id: this.paylist[this.select].id,
					openid: uni.getStorageSync('openid')
				})
				console.log(data)
				const paydata = await this.$apis.order.getWxpay({
					code: data.data.code,
					payType: 2,
					openid: uni.getStorageSync('openid')
				})
				console.log(paydata)
				uni.requestPayment({
					timeStamp: paydata.data.timeStamp,
					nonceStr: paydata.data.nonceStr,
					package: paydata.data.package,
					signType: paydata.data.signType,
					paySign: paydata.data.sign,

					success() {
						uni.showToast({
							title: '充值成功',
							icon: 'none',
							success() {
								setTimeout(() => {
									uni.navigateBack(-1)
								}, 1500)
							}
						})
					},
					fail() {
						uni.showToast({
							title: '取消了充值',
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.spay {
		.headerview {
			background-image: linear-gradient(to right, #363B53, #5B5E74);
			height: 260rpx;
			border-radius: 0% 0% 50% 50% / 0% 0% 82rpx 82rpx;
			overflow: hidden;
			position: relative;

			.huiyuanka {
				background-image: url(https://bhzhtc.oss-cn-beijing.aliyuncs.com/sxsd/31c9e99423ed4c0599932daf385ad4a5.png);
				background-size: 100% 100%;
				background-repeat: no-repeat;
				width: 100%;
				height: 208rpx;
				position: absolute;
				bottom: 0;
				left: 0;
				right: 0;

				text {
					position: absolute;
					bottom: 46rpx;
					color: #40455C;
					left: 72rpx;
				}
			}

		}

		.listbox {
			display: flex;
			justify-content: space-between;
			align-items: center;
			margin: 40rpx 24rpx 0;
			flex-wrap: wrap;

			.list {
				margin-bottom: 40rpx;
				border-radius: 16rpx;
				width: 340rpx;
				height: 232rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				background-color: #FCFCFC;
				font-size: 24rpx;
				color: #999999;

				.price {
					color: #FB4041;
					font-weight: 700;
					font-size: 32rpx;
					height: 74rpx;
					margin: 8rpx 0;

					text {
						font-size: 64rpx;
					}
				}
			}

			.select {
				color: #000000;
				background-color: #FCF5EB;
			}

			&::after {
				content: '';
				width: 340rpx;
			}
		}

		.selectxieyi {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			font-size: 24rpx;
			margin-left: 20rpx;

			.xieyi {
				color: #2FCC71;
			}
		}

		.btnpay {
			position: fixed;
			bottom: 100rpx;
			margin: 0 20rpx;
			width: calc(100% - 40rpx);
			border-radius: 40rpx;
			height: 80rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			background-image: linear-gradient(to right, #FFE5C2, #F1C68E);
		}
	}
</style>

<style lang="scss">
	.uni-navbar__content {
		background-image: linear-gradient(to right, #363B53, #5B5E74);
	}

	label {
		display: flex;
		align-items: center;
	}

	.uni-navbar__header {
		background-color: transparent !important;
	}

	.uni-nav-bar-text {
		font-size: 32rpx !important;
	}
</style>